#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

const int SIZE = 10000;

struct VAL_IDX
{
    int val;
    int idx;
};

void GetRank(int data[][SIZE], int* rank, int id, int N);

int main(int argc, char const *argv[])
{
    freopen("test.txt","r",stdin);
    int N, M;scanf("%d %d",&N,&M);
    int ID[SIZE];
    int data[4][SIZE]{0}; // A,C,M,E
    for(int i = 0;i<N;++i)
    {
        scanf("%d",ID+i);
        int ave=0;
        for(int j = 1;j<=3;++j)
        {
            scanf("%d",data[j]+i);
            ave += data[j][i];
        }
        data[0][i] = ave/3;
    }

    int ranks[4][SIZE];
    for(int i = 0;i<4;++i)  GetRank(data,ranks[i],i,N); 
    map<int, int> id2idx;
    for(int i = 0;i<N;++i)  id2idx[ID[i]] = i; 
    char sub[4] {'A','C','M','E'};

    for(int i = 0;i<M;++i)
    {
        int tmp; scanf("%d",&tmp);
        auto p = id2idx.find(tmp);
        if(p == id2idx.end()) printf("N/A\n");
        else
        {
            int id = p->second;
            int best = SIZE;  
            int bsub = -1;          
            for(int j = 0;j<4;++j)
            {
                if(ranks[j][id] < best) 
                {
                    best = ranks[j][id];
                    bsub = j;
                }
            }
            printf("%d %c\n",best,sub[bsub]);
        }
    }
    return 0;
}

void GetRank(int data[][SIZE], int* rank, int id, int N)
{
    VAL_IDX pre[SIZE];
    for(int i = 0;i<N;++i)
        { pre[i].val = data[id][i]; pre[i].idx = i; }
    auto cmp = [](VAL_IDX a, VAL_IDX b){return a.val>b.val;};
    sort(pre,pre+N,cmp);
    rank[pre[0].idx] = 1;
    for(int i = 1;i<N;++i)
    {
        rank[pre[i].idx] = pre[i].val==pre[i-1].val ? rank[pre[i-1].idx] : i+1;
    }
}
